package work.timejay.user.controller.wechat;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import work.timejay.user.utils.MessageUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @className:MessageController.java
 * @author:王文杰
 * @version:1.0.0
 * @description:微信信息相关控制层
 * @createTime 2022年04月10日 17:55:00
 */
@RestController
@RequestMapping(value = "/api/wechat")
@Slf4j
@Tag(name = "微信消息接口层",description = "MessageController")
public class MessageController {
    /**
     * 接入消息校验
     */
    @GetMapping()
    public String accessMessageCheck(
            @RequestParam(value = "signature") String signature,
            @RequestParam(value = "timestamp") String timestamp,
            @RequestParam(value = "nonce") String nonce,
            @RequestParam(value = "echostr") String echoStr
            //@RequestParam("token") String token
    ) {
        log.info("signature,timestamp,nonce,echoStr,token--->{}-{}-{}-{}-{}", signature, timestamp, nonce, echoStr, "123456");
        //排序
        String[] arrayStr = {"123456", timestamp, nonce};
        arrayStr = MessageUtils.sort(arrayStr);
        //sha1加密
        String sha1EncryptionResult = MessageUtils.sha1Encryption(arrayStr);
        log.info("sha1加密结果--->{}", sha1EncryptionResult);
        //比较
        if (signature.equals(sha1EncryptionResult)) {
            log.info("接入成功");
            return echoStr;
        }
        log.info("接入失败");
        return null;
    }
}
